# 导入 Flask 对象
# https://bootstrapdoc.com/docs/5.0/content/tables
from flask import Flask, render_template, request, redirect
from main_MAC import *
error=''
error2=''
data1=[]
data2=[]
data1_1=[]
data1_2=[]
R_content = []
R_content2=[]
Write_op=0
Write_op2=0

# 使用 Flask 对象创建一个 app 对象
app = Flask(__name__)

"""功能函数"""
def get_content_info(obj,content):
    global R_content
    R_content=[]
    R_content.append((obj,content))
def get_content_info2(obj,content):
    global R_content2
    R_content2=[]
    R_content2.append((obj,content))
   
# 路由

@app.route('/')  # / 访问的路径
def index():
    msg = 'Hello! Welcome to MAC(BLP) Model!'
    return render_template("index.html",data=msg)


@app.route('/login', methods=['GET', 'POST'])
def login():
    # 登录的功能
    # 全栈项目，前后端不分离
    # return '需要实现登录的逻辑'
    # request 对象可以拿到浏览器传递给服务器的所有数据
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        # 登录成功之后，链接数据库，校验账号密码
        print('从服务器接收到的数据：', username, password)
        # 登录成功之后，应该调转到管理页面
        if username=='admin' and password == '123456':
            return redirect('/admin')
        elif username =='admin2' and password == '123456':
            return redirect('/admin2')
    return render_template('login.html')

@app.route('/admin')
def admin():
    # 重点 复制框架源码
    global data1,data2
    data1 = make_change_table()
    data2 = make_change_table2()
    run_init(data1,data2)
    return render_template('admin.html',info=info_list)
@app.route('/admin2')
def admin2():
    # 重点 复制框架源码
    global data1_1,data1_2
    data1_1 = make_change_table1_2()
    data1_2 = make_change_table2_2()
    run_init2(data1_1,data1_2)
    return render_template('admin2.html',info=info_list2)
    
@app.route('/error')
def error_():
    global error
    error = "error1"
    return render_template('error.html',error = error)
@app.route('/error2')
def error_2():
    global error2
    error2 = "error2"
    return render_template('error2.html',error = error2)

@app.route('/delete')
def delete():
    sub_get = request.args.get('sub')
    obj_get = request.args.get('obj')
    for info in info_list:
        if (sub_get=='root1'and obj_get=='core_file1') or (sub_get=='root2'and obj_get=='core_file2') or (sub_get=='root3'and obj_get=='core_file3') :
            return redirect('/error')
        if info['sub'] == sub_get and info['obj'] == obj_get:
            info_list.remove(info)
            break
    return redirect('/admin')  
            
@app.route('/delete2')
def delete2():
    sub_get = request.args.get('sub')
    obj_get = request.args.get('obj')
    for info in info_list2:
        if (sub_get=='root1'and obj_get=='core_file1') or (sub_get=='root2'and obj_get=='core_file2') or (sub_get=='root3'and obj_get=='core_file3') :
           return redirect('/error2')
        if info['sub'] == sub_get and info['obj'] == obj_get:
            info_list2.remove(info)
            break
    return redirect('/admin2')

@app.route('/add', methods=["GET", "POST"])
def add():
    if request.method == 'POST':
        sub_read = request.form.get('sub')
        obj_read = request.form.get('obj')
        sub_level_read = request.form.get('sub_level')
        obj_level_read = request.form.get('obj_level')
        print('获取的用户信息:', sub_read, obj_read, sub_level_read, obj_level_read)
        f = input_info(sub_read,obj_read, sub_level_read, obj_level_read)
        if f == 1:
            return redirect('/admin')
        else:
            return redirect('/error')
    return render_template('add.html')
@app.route('/add2', methods=["GET", "POST"])
def add2():
    if request.method == 'POST':
        sub_read = request.form.get('sub')
        obj_read = request.form.get('obj')
        sub_level_read = request.form.get('sub_level')
        obj_level_read = request.form.get('obj_level')
        print('获取的用户信息:', sub_read, obj_read, sub_level_read, obj_level_read)
        f = input_info2(sub_read,obj_read, sub_level_read, obj_level_read)
        if f == 1:
            return redirect('/admin2')
        else:
            return redirect('/error2')
    return render_template('add2.html')

'''修改主体安全许可'''
@app.route('/change', methods=["GET", "POST"])
def change_sublevel():
    if request.method == "POST":
        sub_change = request.form.get('sub')
        sub_new_level = request.form.get('sub_level')
        print('获取的修改安全许可信息：', sub_change, " ",sub_new_level)
        f = change_sub_level(sub_change,sub_new_level)
        print(info_list)
        if f == 0:
            return redirect('/admin')
        else:
            return redirect('/error')
    return render_template('change.html')
@app.route('/change_2', methods=["GET", "POST"])
def change_sublevel2():
    if request.method == "POST":
        sub_change = request.form.get('sub')
        sub_new_level = request.form.get('sub_level')
        print('获取的修改安全许可信息：', sub_change, " ",sub_new_level)
        f = change_sub_level2(sub_change,sub_new_level)
        print(info_list2)
        if f == 0:
            return redirect('/admin2')
        else:
            return redirect('/error2')
    return render_template('change_2.html')


'''修改客体安全等级'''
@app.route('/change1', methods=["GET", "POST"])
def change_objlevel():
    if request.method == "POST":
        obj_change = request.form.get('obj')
        obj_new_level = request.form.get('new_obj_level')
        print('获取的修改安全许可信息：', obj_change, " ",obj_new_level)
        f = change_obj_level(obj_change,obj_new_level)
        print(info_list)
        if f == 0:
            return redirect('/admin')
        else:
            return redirect('error')
    return render_template('change1.html')
@app.route('/change1_2', methods=["GET", "POST"])
def change_objlevel2():
    if request.method == "POST":
        obj_change = request.form.get('obj')
        obj_new_level = request.form.get('new_obj_level')
        print('获取的修改安全许可信息：', obj_change, " ",obj_new_level)
        f = change_obj_level2(obj_change,obj_new_level)
        print(info_list2)
        if f == 0:
            return redirect('/admin2')
        else:
            return redirect('error2')
    return render_template('change1_2.html')


'''修改文件内容'''
@app.route('/change2', methods=["GET", "POST"])
def change_filecontent():
    global Write_op
    if request.method == "POST":
        sub_change_file = request.form.get('sub')
        obj_change_file = request.form.get('obj')
        op_change_file = request.form.get('op')
        change_content = request.form.get('content')
        print('获取的修改安全许可信息：', sub_change_file," ",obj_change_file, " ",op_change_file," ",change_content)
        Write_op = change_obj_info_file(sub_change_file,obj_change_file,op_change_file,change_content)
        print("Write_op:",Write_op)
        if Write_op == 1:
            return redirect('/admin')
        else:
            return redirect('/error')
    return render_template('change2.html')
@app.route('/change2_2', methods=["GET", "POST"])
def change_filecontent2():
    global Write_op2
    if request.method == "POST":
        sub_change_file = request.form.get('sub')
        obj_change_file = request.form.get('obj')
        op_change_file = request.form.get('op')
        change_content = request.form.get('content')
        print('获取的修改安全许可信息：', sub_change_file," ",obj_change_file, " ",op_change_file," ",change_content)
        Write_op2 = change_obj_info_file2(sub_change_file,obj_change_file,op_change_file,change_content)
        print("Write_op:",Write_op2)
        if Write_op2 == 1:
            return redirect('/admin2')
        else:
            return redirect('/error2')
    return render_template('change2_2.html')

'''读取文件内容'''
@app.route('/read', methods=["GET", "POST"])
def read_file():
    global flag
    if request.method == "POST":
        sub_read = request.form.get('sub')
        obj_read = request.form.get('obj')
        option_read = request.form.get('option_read')
        print('获取的修改安全许可信息：', sub_read, " ",obj_read," ",option_read)
        (flag,content_read_file)=read_obj_info_file(sub_read,obj_read,option_read)
        print("flag:",flag)
        print("content_read_file:",content_read_file)
        if flag == 1:##表示能读
            get_content_info(obj_read,content_read_file)
            return redirect('/read_info')
        else:
            return redirect('/error')
    return render_template('read.html')
@app.route('/read2', methods=["GET", "POST"])
def read_file2():
    global flag2
    if request.method == "POST":
        sub_read = request.form.get('sub')
        obj_read = request.form.get('obj')
        option_read = request.form.get('option_read')
        print('获取的修改安全许可信息：', sub_read, " ",obj_read," ",option_read)
        (flag2,content_read_file)=read_obj_info_file2(sub_read,obj_read,option_read)
        print("flag:",flag2)
        print("content_read_file:",content_read_file)
        if flag2 == 1:##表示能读
            get_content_info2(obj_read,content_read_file)
            return redirect('/read_info2')
        else:
            return redirect('/error2')
    return render_template('read2.html')



'''展示文件内容'''
@app.route('/read_info')
def read_info():
    print("R_content:",R_content)
    return render_template('read_info.html',read = R_content)
@app.route('/read_info2')
def read_info2():
    print("R_content2:",R_content2)
    return render_template('read_info2.html',read = R_content2)

if __name__ == '__main__':
    app.run()